<!-- 新增分区 -->
<template>
  <div>
    <el-dialog title="申请付款" :visible.sync="dialogShow" width="65%" :close-on-click-modal="false" :close-on-press-escape="false">
      <form-common v-if="applyFiledList.length > 0" :filed-list="applyFiledList" ref="applyForm"></form-common>
      <div class="flex margin-bottom-20">
        <p class="left-lable font-size-16 margin-top-5 required font-weight-400 color-333333" style="width: 158px">审批文件</p>
        <upload-file :multiple="true" class="upload-box" :limit="10" tip="" :max-size="100" accept=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.pdf" :fileList.sync="fileList"> </upload-file>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false" class="el-button-reset">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import formCommon from '@g/components/form/formCommon.vue';
  import { applyFiledList } from '../form.js';
  import { guid, requestTips } from '@/common/js/utils';
  import { apiAddPayRecordV2 } from '../../../../api/contract';
  import { apiCreateAudit } from '../../../../api/common';

  export default {
    props: {
      contractInfo: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    data() {
      return {
        dialogShow: false,
        applyFiledList: [],
        fileList: [],
        userInfo: JSON.parse(localStorage.getItem('userInfo')),
        chargeMap: {},
      };
    },

    methods: {
      async confirmFn() {
        let model = this.$refs.applyForm.validateForm(); //基本信息
        if (!model) return;
        model.projectKey = this.contractInfo.projectKey;
        model.applyUser = this.userInfo.key;
        model.contractKey = this.$route.query.contractKey;
        model.contractCode = this.$route.query.contractCode;
        model.attachmentList = this.getFileList();
        model.recordStatus = 1;
        model.fundsType = 3; //(1,收款 2,付款, 3 付款申请)
        if (model.bizId) {
          model.chargeItem = this.chargeMap[model.bizId] ? this.chargeMap[model.bizId].chargeItemCode : '';
          model.chargeItemName = this.chargeMap[model.bizId] ? this.chargeMap[model.bizId].chargeItemAliasName : '';
        }
        let res = await apiAddPayRecordV2(model);
        if (res.success) {
          this.submitFn(res.key);
        } else {
          this.$message.error(res.message);
        }
      },

      //初始化一些值
      async initFieldVal(unpaidAmount, acceptanceList, chargeList) {
        let chargeMap = {};

        if (chargeList) {
          chargeList.forEach(item => {
            chargeMap[item.chargeItemKey] = item;
          });
        }
        this.chargeMap = chargeMap;

        this.applyFiledList = this.lodash.cloneDeep(applyFiledList);

        await this.$nextTick(() => {});
        this.$refs.applyForm.clearValidateForm();
        this.applyFiledList[0].bindFieldVal = this.contractInfo.projectName;
        // this.applyFiledList[1].bindFieldVal = this.contractInfo.orgName;
        this.applyFiledList[1].bindFieldVal = this.userInfo.nickname;
        this.applyFiledList[2].bindFieldVal = this.contractInfo.contractName;
        this.applyFiledList[3].bindFieldVal = unpaidAmount || '0';
        this.applyFiledList[5].optionList = chargeList;
        this.applyFiledList[6].optionList = acceptanceList;
      },

      //提审
      async submitFn(key) {
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let param = {
          businessKey: key, //废止合同保存的主键
          userPrimaryKey: userInfo.key,
          projectKey: this.contractInfo.projectKey,
          type: '付款', //合同,账单,付款,合同终止
          typeKey: 'payment', //contractSubmit,bill,payment,contractTermination
        };
        let res = await apiCreateAudit(param);
        requestTips(res, async () => {
          this.$emit('getInfo');
          this.dialogShow = false;
        });
      },

      // 设置附件列表
      getFileList() {
        if (!this.fileList || this.fileList.length == 0) return [];
        let list = [];
        this.fileList.forEach(item => {
          if (item.response && item.response.data.rows.length > 0) {
            let fileObj = item.response.data.rows[0];
            list.push({
              fileId: fileObj.key,
              fileUrl: fileObj.url,
              fileTitle: item.name,
              fileDesc: item.name,
            });
          }
        });
        return list;
      },

      //回显赋值
      backFileInfo(fileList) {
        this.fileList = fileList;
      },
    },

    components: { formCommon },
  };
</script>
<style lang="scss" scoped>
  .delete-btn {
    padding-top: 0;
  }
</style>
